home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 9 / Example 9.2 / building.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2006-06-30  |  3.6 KB  |  150 lines

  1. #include "building.h"
  2.  
  3. std::vector<MESH*> buildingMeshes;
  4.  
  5. void LoadBuildingResources(IDirect3DDevice9* Device)
  6. {
  7.     std::vector<std::string> fnames;
  8.  
  9.     fnames.push_back("meshes/townhall.x");
  10.     fnames.push_back("meshes/barracks.x");
  11.     fnames.push_back("meshes/tower.x");
  12.  
  13.     for(int i=0;i<fnames.size();i++)
  14.         buildingMeshes.push_back(new MESH((char*)fnames[i].c_str(), Device));
  15. }
  16.  
  17. void UnloadBuildingResources()
  18. {
  19.     for(int i=0;i<buildingMeshes.size();i++)
  20.         if(buildingMeshes[i] != NULL)
  21.             delete buildingMeshes[i];
  22.  
  23.     buildingMeshes.clear();
  24. }
  25.  
  26. bool PlaceOk(int buildType, INTPOINT mp, TERRAIN *terrain)
  27. {
  28.     if(terrain == NULL)return false;
  29.  
  30.     BUILDING b(buildType, mp, NULL, false, NULL);
  31.     RECT r = b.GetMapRect(1);
  32.  
  33.     for(int y=r.top;y<=r.bottom;y++)
  34.         for(int x=r.left;x<=r.right;x++)
  35.         {
  36.             //Building must be within map borders
  37.             if(!terrain->Within(INTPOINT(x,y)))return false;
  38.             MAPTILE *tile = terrain->GetTile(x, y);
  39.             if(tile == NULL)return false;
  40.  
  41.             //The terrain must be level and walkable
  42.             if(tile->m_height != 0.0f || !tile->m_walkable)return false;
  43.         }
  44.  
  45.     return true;
  46. }
  47.  
  48. //////////////////////////////////////////////////////////////////////////////////
  49. //                                BUILDING                                        //
  50. //////////////////////////////////////////////////////////////////////////////////
  51.  
  52. BUILDING::BUILDING(int _type, INTPOINT mp, TERRAIN *_terrain, bool _affectTerrain, IDirect3DDevice9* Dev)
  53. {
  54.     m_type = _type;
  55.     m_mappos = mp;
  56.     m_pTerrain = _terrain;
  57.     m_affectTerrain = _affectTerrain;
  58.     m_pDevice = Dev;    
  59.     m_team = 0;
  60.     m_range = m_damage = 0;
  61.     m_meshInstance.SetMesh(buildingMeshes[m_type]);
  62.  
  63.     if(m_type == 0)        //Townhall
  64.     {
  65.         m_hp = m_hpMax = 600;
  66.         m_sightRadius = 10;
  67.         m_name = "Townhall";
  68.         m_mapsize.Set(4,2);
  69.         m_meshInstance.SetScale(D3DXVECTOR3(0.13f, 0.13f, 0.13f));
  70.     }    
  71.     else if(m_type == 1)        //Barracks
  72.     {
  73.         m_hp = m_hpMax = 450;
  74.         m_sightRadius = 8;
  75.         m_name = "Barracks";
  76.         m_mapsize.Set(2,4);
  77.         m_meshInstance.SetScale(D3DXVECTOR3(0.15f, 0.15f, 0.15f));
  78.     }    
  79.     else if(m_type == 2)        //Tower
  80.     {
  81.         m_hp = m_hpMax = 750;
  82.         m_sightRadius = 15;
  83.         m_name = "Tower";
  84.         m_mapsize.Set(2,2);
  85.         m_meshInstance.SetScale(D3DXVECTOR3(0.13f, 0.13f, 0.13f));
  86.     }
  87.  
  88.     if(m_pTerrain != NULL)
  89.         m_position = m_pTerrain->GetWorldPos(m_mappos) + D3DXVECTOR3(m_mapsize.x / 2.0f, 0.0f, -m_mapsize.y / 2.0f);
  90.     else m_position = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
  91.  
  92.     m_meshInstance.SetPosition(m_position);
  93.  
  94.     m_BBox = m_meshInstance.GetBoundingBox();
  95.     m_BBox.max -= D3DXVECTOR3(0.2f, 0.2f, 0.2f);
  96.     m_BBox.min += D3DXVECTOR3(0.2f, 0.2f, 0.2f);
  97.  
  98.     //Update the tiles of the terrain
  99.     if(m_pTerrain != NULL && m_affectTerrain)
  100.     {
  101.         RECT mr = GetMapRect(0);
  102.  
  103.         for(int y=mr.top;y<=mr.bottom;y++)
  104.             for(int x=mr.left;x<=mr.right;x++)
  105.             {
  106.                 MAPTILE *tile = m_pTerrain->GetTile(x, y);
  107.                 if(tile != NULL)tile->m_walkable = false;
  108.             }
  109.  
  110.         m_pTerrain->UpdatePathfinding(&GetMapRect(1));
  111.     }
  112. }
  113.  
  114. BUILDING::~BUILDING()
  115. {
  116.     //restore the tiles of the terrain
  117.     if(m_pTerrain != NULL && m_affectTerrain)
  118.     {
  119.         RECT mr = GetMapRect(0);
  120.  
  121.         for(int y=mr.top;y<=mr.bottom;y++)
  122.             for(int x=mr.left;x<=mr.right;x++)
  123.             {
  124.                 MAPTILE *tile = m_pTerrain->GetTile(x, y);
  125.                 if(tile != NULL)tile->m_walkable = true;
  126.             }
  127.  
  128.         m_pTerrain->UpdatePathfinding(&GetMapRect(1));
  129.     }
  130. }
  131.  
  132. void BUILDING::Render()
  133. {
  134.     m_meshInstance.Render();
  135. }
  136.  
  137. void BUILDING::Update(float deltaTime)
  138. {
  139.     //Train units, upgrade things etc here...
  140. }
  141.  
  142. BBOX BUILDING::GetBoundingBox()
  143. {
  144.     return m_BBox;
  145. }
  146.  
  147. D3DXMATRIX BUILDING::GetWorldMatrix()
  148. {
  149.     return m_meshInstance.GetWorldMatrix();
  150. }